<?php
//------------------------------------------------------------------------------
    include_once($config['app_dir'] . '/includes/object.class.php');
    include_once($config['app_dir'] . '/includes/template.class.php');
	include_once($config['app_dir'] . '/includes/db.class.php');
	include_once($config['app_dir'] . '/includes/FormParams.class.php');
    //----------------------------------------------------------------------
    class objEditForm extends objObject {
            private $Params = false;
            private $Form = false;
            public $field_id = false;
            public $field_value = false;
            public $table = false;

            private $db;

        function __construct() {
            $this->db = objDataBase::GetInstance();
            $this->Params = new objFormParams('post', 'data_');
            $this->Form = new objTemplate();
        }//constructor

        public function ClassName() {
            return  __CLASS__;
        }//ClassName

        function getUID() {
            $id = strtolower($this->Params->getItem($this->field_id));

            if ( !$this->field_value && !$this->Params->isEmpty() && 'null' != $id )
                $this->field_value = $id;

            if (!$this->field_id || !$this->field_value || is_null($this->field_value)) return false;
                else  return $this->field_id.'='.$this->field_value;
        }//getuid

        function run() {
            $result = false;

            if (!$this->field_id || !$this->table) return $result;
            try {
                ADOdb_Active_Record::SetDatabaseAdapter( $this->db->handler );
                $record = new ADODB_Active_Record($this->table);
            } catch(exception $e){
                echo $e->getMessage();
            }//catch

            if ($uid = $this->getUID() ) {
                try {
                   $record->load($uid);
                } catch(exception $e){
                   echo $e->getMessage();
                }//catch
            }
            //----------------------------------------------------------------------------------------------
            // Check params from from
            //----------------------------------------------------------------------------------------------
            if ( !$this->Params->isEmpty() )  { //if params   pass from from
                $params_arr = $this->Params->getAssocArray(true); // get assoc array from params
                if ( array_key_exists($this->field_id, $params_arr) && 'null' ==strtolower($params_arr[$this->field_id]) )    unset($params_arr[$this->field_id]);
                //----------------------------------------------------------------------------------------------
                try {
                    $this->db->handler->StartTrans();//start transaction
                    //----------------------------------------------------------------------------------------------
                    foreach (get_object_vars($record) as $prop => $val) { //set properies of acrive reocd
                        if ( array_key_exists($prop, $params_arr) )
                            $record->{$prop} = $params_arr[$prop];
                    }
                    $record->save(); //save changes

                    $result = $this->db->handler->CompleteTrans();//finish transaction
                    $this->field_value = $record->{$this->field_id};

                    $uid = $this->getUID();

                } catch(exception $e){
                   echo $e->getMessage();
                }
            } //if set parameters
            $this->Form->assign_by_ref("record", $record); //pass data into template
            return $result;
        }//run

        function fetchForm($template_file) {
            return $this->Form->fetch($template_file);
        }//fetchForm

    }//class objEditFrom
//------------------------------------------------------------------------------
?>
